/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openmrs.module.simplelabentry.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openmrs.Order;
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;
import org.openmrs.module.hospitalcore.LabService;
import org.openmrs.module.hospitalcore.model.Lab;
import org.openmrs.module.hospitalcore.model.LabTest;
import org.openmrs.module.hospitalcore.util.HospitalCoreConstants;
import org.openmrs.module.simplelabentry.SimpleLabEntryService;

/**
 *
 * @author Saptarshi
 */
public class AcceptLabOrderServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	System.out.println("in acceptorder servlet");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        
        try {
        	if( !Context.isAuthenticated()){
             	 out.print("unauthenticated");
            }else {
           
	            String orderId = request.getParameter("orderId");
	            if (orderId != null && !orderId.equals("")) {
	                int id = Integer.parseInt(orderId);
	                OrderService os = Context.getOrderService();
	                SimpleLabEntryService ls = Context.getService(SimpleLabEntryService.class);
	                LabService labService = Context.getService(LabService.class);
	                Order order = os.getOrder(id);
	                if (order != null) {
	                    String accept = request.getParameter("accept");
	                    if (accept != null && accept.equals("true")) {
	                    	// Create LabTest
	
	                    	Lab lab = ls.getCurrentLab();
	                    	LabTest labtest = new LabTest();
	                    	Calendar now = Calendar.getInstance();
	                    	labtest.setAcceptDate(now.getTime());
	                    	labtest.setOrder(order);
	                    	labtest.setLab(lab);
	                    	String sampleNumber = labService.getNextSampleNumber(lab, now.getTime());
	                    	labtest.setSampleNumber(sampleNumber);
	                    	labtest.setLabTestStatus(HospitalCoreConstants.LABTEST_STATUS_ACCEPTED);
	                        labService.saveLabTest(labtest);	                        
	                        
	                        order.setAccessionNumber("1");
	                    	os.saveOrder(order);
	                        out.print(sampleNumber);
	                    } else if (accept != null && accept.equals("false")) {
	                        order.setAccessionNumber("0");
	                        labService.deleteLabTestByOrder(order);
	                        os.saveOrder(order);
	                        //out.print("lock.gif");
	                    }
	                }
	            }
            }
        }catch (Exception e){
        	e.printStackTrace();
        }finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
    	System.out.println("get info");
        return "Accept Orders";
    }// </editor-fold>
}

